package com.yzqp.servergoods.server;


import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.yzqp.servergoods.dao.GoodsDao;
import com.yzqp.servergoods.entity.GoodsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class GoodsServer {

    @Value("${server.port}")
    String port;

    int i = 0;

    @Autowired
    GoodsDao goodsDao;


    @LcnTransaction()
    @Transactional
    public String updateStock(int id, int count){

        // 记录访问次数
        i ++;
        System.out.println("端口 : " + this.port + " 次数: " + this.i + " .................");

        GoodsEntity goods = goodsDao.findById(id).get();
        goods.setStock(goods.getStock() - count);
        goodsDao.save(goods);
        if(count < 0){
            return "goodsError";
        }
        return "success";
    }
}
